package com.wang.system.controller;

import com.wang.common.core.constants.HttpConstants;
import com.wang.common.core.controller.BaseController;
import com.wang.common.core.domain.R;
import com.wang.system.domain.sysuser.dto.LoginDTO;
import com.wang.system.domain.sysuser.dto.SysUserAddDTO;
import com.wang.system.domain.sysuser.vo.LoginUserVO;
import com.wang.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "管理员用户功能接口")
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService service;


    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody @Validated LoginDTO loginDTO){
        // @RequestBody 用于将 json数据 转换为 实体对象.
        // @Validated 用于参数校验
        return service.login(loginDTO);
    }


    @Operation(summary = "新增管理员用户")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Validated SysUserAddDTO sysUserAddDTO){
        return BaseController.toR(service.add(sysUserAddDTO));
    }

    @GetMapping("/info")
    @Operation(summary = "查询管理员用户信息")
    public R<LoginUserVO> querySysUserInfo(@RequestHeader(HttpConstants.AUTHENTICATION) String token){

        //@RequestHeader --> 获取请求头中的信息
        return service.querySysUserInfo(token);
    }

    @Operation(summary = "管理员退出登录")
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return BaseController.toR(service.logout(token));
    }
}